//
//  AddBalanceController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/14.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "AddBalanceController.h"
#import "AddBalanceAndExtractListVC.h"
#import "AddBalanceDetailsVC.h"

@interface AddBalanceController ()
/**输入框*/
@property (nonatomic,strong)UITextField*textFiled;
/**支付选择按钮*/
@property (nonatomic,strong)UIButton*selectZFB;
/**微信选择按钮*/
@property (nonatomic,strong)UIButton*selectWX;
@end

@implementation AddBalanceController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xF5F7F5);
    [self createUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self.textFiled becomeFirstResponder];
}
-(void)setType:(AddBalanceType)type{
    _type = type;
    if (type == AddBalance) {
        self.title = @"余额充值";
        [self addNavigationItemWithTitles:@[@"充值记录"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    }else{
        self.title = @"提现";
        [self addNavigationItemWithTitles:@[@"提现记录"] isLeft:NO target:self action:@selector(rightAction) tags:nil];
    }
}
-(void)rightAction{
    AddBalanceAndExtractListVC * vc = [AddBalanceAndExtractListVC new];
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)pushDateilController{
    AddBalanceDetailsVC * vc = [AddBalanceDetailsVC new];
    vc.type = self.type;
    [self.navigationController pushViewController:vc animated:YES];

}
-(void)createUI{
    UIView * view = UIView.new;
    view.backgroundColor = UIColor.whiteColor;
    
    UILabel * titleLabel = UILabel.new;
    titleLabel.textColor = UIColorFromRGB(0x273323);
    titleLabel.font = SYSTEMFONT(18);
    
    UILabel * iconLabel = UILabel.new;
    iconLabel.textColor = UIColorFromRGB(0x273323);
    iconLabel.font = BOLDSYSTEMFONT(50);
    iconLabel.text = @"¥";
    
    UIView * oneLine = UIView.new;
    oneLine.backgroundColor = UIColorFromRGB(0xE7EEE7);
    
    UILabel * leftName = UILabel.new;
    leftName.textColor = UIColorFromRGB(0x273323);
    leftName.font = SYSTEMFONT(18);
    leftName.text = @"支付方式";

    UIView * twoLine = UIView.new;
    twoLine.backgroundColor = UIColorFromRGB(0xE7EEE7);
    
    UIView * zfbView = [self createViewTitle:@"zfb_pay" name:@"支付宝支付" selectButton:self.selectZFB];
    
    UIView * thereLine = UIView.new;
    thereLine.backgroundColor = UIColorFromRGB(0xE7EEE7);

    UIView * wxView = [self createViewTitle:@"wx_pay" name:@"微信支付" selectButton:self.selectWX];
    
    UIButton * commitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commitButton setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
    commitButton.titleLabel.font = PingFangSC_Medium(18);
    ViewRadius(commitButton, 5);
    
    if (self.type == AddBalance) {
        titleLabel.text = @"充值金额";
        [commitButton setTitle:@"立即充值" forState:UIControlStateNormal];
    }else{
        titleLabel.text = @"提取金额";
        [commitButton setTitle:@"立即提取" forState:UIControlStateNormal];

    }
    kWeakSelf(weakSelf);
    [commitButton addActionHandler:^{
        [weakSelf pushDateilController];
    }];


    [self.view addSubview:view];
    
    [self.view addSubview:commitButton];
    
    [view addSubview:titleLabel];
    [view addSubview:iconLabel];
    [view addSubview:self.textFiled];
    [view addSubview:oneLine];
    [view addSubview:leftName];
    [view addSubview:twoLine];
    [view addSubview:zfbView];
    [view addSubview:thereLine];
    [view addSubview:wxView];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(48);
        make.height.mas_equalTo(324);
    }];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(20);
    }];
    
    [iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(30);
        make.width.mas_equalTo(30);
    }];
    
    [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(iconLabel.mas_right).offset(5);
        make.centerY.mas_equalTo(iconLabel);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    [oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.textFiled.mas_bottom).offset(5);
    }];
    
    [leftName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(oneLine.mas_bottom).offset(20);
    }];
    
    [twoLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(leftName.mas_bottom).offset(20);
    }];
    
    [zfbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(65);
        make.top.mas_equalTo(twoLine.mas_bottom).offset(0);
    }];
    [thereLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(zfbView.mas_bottom).offset(0);
    }];
    [wxView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(65);
        make.top.mas_equalTo(thereLine.mas_bottom).offset(0);
    }];

    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.right.mas_equalTo(-40);
        make.bottom.mas_equalTo(-50);
        make.height.mas_equalTo(50);
    }];
}

-(UITextField*)textFiled{
    if(!_textFiled){
        _textFiled=[UITextField new];
        _textFiled.placeholder = @"0";
        _textFiled.font = BOLDSYSTEMFONT(50);
//        _textFiled.keyboardType = UIKeyboardTypeNumberPad;
        FJCustomKeyboardView *keyboardView = [[FJCustomKeyboardView alloc] init];
        if (self.type == AddBalance) {
            keyboardView.completeTitle = @"充值";
        }else{
            keyboardView.completeTitle = @"提取";
        }
        _textFiled.inputView = keyboardView;
        
        kWeakSelf(weakSelf);
        keyboardView.backBlock = ^{
            [weakSelf pushDateilController];
        };

    }
    return _textFiled;
}
-(UIView *)createViewTitle:(NSString *)imageStr name:(NSString *)nameStr selectButton:(UIButton *)button{
    UIView * view = UIView.new;
    UIImageView * imageView = [[UIImageView alloc]initWithImage:IMAGE_NAMED(imageStr)];
//    imageView.backgroundColor = KGrayColor;
    imageView.layer.cornerRadius = 18;
    
    UILabel * label = UILabel.new;
    label.textColor = UIColorFromRGB(0x273323);
    label.font = SYSTEMFONT(16);
    label.text = nameStr;
    
    [view addSubview:imageView];
    [view addSubview:label];
    [view addSubview:button];
    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.width.height.mas_equalTo(36);
    }];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(imageView.mas_right).offset(10);
    }];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.width.height.mas_equalTo(16);
    }];
    
    
    return view;
}
-(UIButton*)selectZFB{
    if(!_selectZFB){
        _selectZFB=[UIButton buttonWithType:UIButtonTypeCustom];
        [_selectZFB setBackgroundImage:IMAGE_NAMED(@"me_check_pay_type_defut") forState:UIControlStateNormal];
        [_selectZFB setBackgroundImage:IMAGE_NAMED(@"me_check_pay_type_selecet") forState:UIControlStateSelected];
        _selectZFB.selected = YES;
        kWeakSelf(weakSelf);
        [_selectZFB addActionHandler:^{
            weakSelf.selectWX.selected=NO;
            weakSelf.selectZFB.selected = YES;
        }];

    }
    return _selectZFB;
}
-(UIButton*)selectWX{
    if(!_selectWX){
        _selectWX=[UIButton buttonWithType:UIButtonTypeCustom];
        [_selectWX setBackgroundImage:IMAGE_NAMED(@"me_check_pay_type_defut") forState:UIControlStateNormal];
        [_selectWX setBackgroundImage:IMAGE_NAMED(@"me_check_pay_type_selecet") forState:UIControlStateSelected];
        _selectWX.selected = NO;
        
        kWeakSelf(weakSelf);
        [_selectWX addActionHandler:^{
            weakSelf.selectWX.selected=YES;
            weakSelf.selectZFB.selected = NO;
        }];
        
    }
    return _selectWX;
}

@end
